package com.lv.phuotbacnam.ui.fragment;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.lv.phuotbacnam.R;
import com.lv.phuotbacnam.bl.queries.QueriesConstant.TableUser;
import com.lv.phuotbacnam.ui.Section;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.parse.ParseFacebookUtils;
import com.parse.ParseUser;

/**
 * Fragment incorporating the left menu
 */
public class LeftMenuFragment extends BaseFragment {
		public ImageLoader imageLoader;
        private List<Section> menuSections;

        public LeftMenuFragment() {
                menuSections = new ArrayList<Section>();
                menuSections.add(Section.CREATE_NEW_BAI_DANG);
                menuSections.add(Section.KICKONS);
                menuSections.add(Section.SETTINGS);
                menuSections.add(Section.INVITE);
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                        Bundle savedInstanceState) {

                final View rootView = inflater.inflate(R.layout.fragment_left_menu,
                                container, false);

                ImageView profileImage = (ImageView) rootView
                                .findViewById(R.id.left_menu_pic);

                ParseUser user = ParseUser.getCurrentUser();
                imageLoader = ImageLoader.getInstance();
                imageLoader.displayImage(user.getString(TableUser.COLUMN_photoURL),
                                profileImage);

                TextView profileFullName = (TextView) rootView
                                .findViewById(R.id.left_menu_fullname);
                profileFullName.setText(user.getString(TableUser.COLUMN_firstName)
                                + " " + user.getString(TableUser.COLUMN_lastName));

                // create ListView personal information
                ListView lv = (ListView) rootView.findViewById(R.id.left_menu_list);

                ArrayList<Model> models = new ArrayList<Model>();
                models.add(new Model(R.drawable.thongbao, "ThĂ´ng bĂ¡o má»›i", "1"));
                models.add(new Model(R.drawable.thongtincanhan, "ThĂ´ng tin cĂ¡ nhĂ¢n",
                                "2"));
                //models.add(new Model(R.drawable.thietlap, "Thiáº¿t láº­p riĂªng tÆ°", ""));
                models.add(new Model(R.drawable.diadiem, "Ä�á»‹a Ä‘iá»ƒm gáº§n Ä‘Ă¢y", ""));
                // models.add(new Model(R.drawable.dieukhoan,"Ä�iá»�u khoáº£n sá»­ dá»¥ng",""));
                models.add(new Model(R.drawable.baocao, "Ä�á»‹a Ä‘iá»ƒm Ä‘Ă£ Ä‘i", ""));
                models.add(new Model(R.drawable.dangxuat, "Ä�Äƒng xuáº¥t", ""));

                MyAdapter adapter = new MyAdapter(rootView.getContext(), models);
                lv.setAdapter(adapter);
                // event click for item
                lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                        long arg3) {
                                // thong bao moi
                                if (arg2 == 0) {
                                        Intent intent = new Intent(rootView.getContext(),
                                                        thongbaomoi.class);
                                        startActivity(intent);
                                }
                                // thong tin ca nhan
                                if (arg2 == 1) {
                                        Intent intent = new Intent(rootView.getContext(),
                                                        thongtincanhan.class);
                                        startActivity(intent);
                                }
                                // thiet lap rieng tu
//                              if (arg2 == 2) {
//                                      Intent intent = new Intent(rootView.getContext(),
//                                                      thietlapriengtu.class);
//                                      startActivity(intent);
//                              }
                                // dia diem gan day
                                if (arg2 == 2) {
                                        Intent intent = new Intent(rootView.getContext(),
                                                        diadiemganday.class);
                                        startActivity(intent);
                                }
                                // dia diem da di
                                if (arg2 == 3) {
                                        Intent intent = new Intent(rootView.getContext(),
                                                        diadiemdadi.class);
                                        startActivity(intent);
                                }
                                // dang xuat
                                if (arg2 == 4) {
                                        ParseFacebookUtils.getSession()
                                                        .closeAndClearTokenInformation();
                                        ParseUser.logOut();

                                        getNavigationController().getNavigationMenu().hideMenu();
                                        getNavigationController().navigateTo(StartFragment.class,
                                                        Section.NONE, null, false);
                                }

                        }
                });

                /*
                 * // Goto new bai dang
                 * rootView.findViewById(R.id.new_bai_dang).setOnClickListener( new
                 * OnClickListener() {
                 * 
                 * @Override public void onClick(View v) { // Bundle bundle = new
                 * Bundle(); //
                 * bundle.putSerializable(Constants.KEY_INTENT_FOR_FRAGMENT, //
                 * Section.CREATE_NEW_BAI_DANG); getNavigationController().navigateTo(
                 * BackActivity.class, false, Section.CREATE_NEW_DIA_DIEM, null); } });
                 * 
                 * 
                 * // To logout
                 * rootView.findViewById(R.id.logout_button).setOnClickListener( new
                 * OnClickListener() {
                 * 
                 * @Override public void onClick(View arg0) {
                 * 
                 * ParseFacebookUtils.getSession() .closeAndClearTokenInformation();
                 * ParseUser.logOut();
                 * 
                 * getNavigationController().getNavigationMenu() .hideMenu();
                 * getNavigationController().navigateTo( StartFragment.class,
                 * Section.NONE, null, false); } });
                 */

                // ListView optionsView = (ListView) rootView
                // .findViewById(R.id.left_menu_list);
                // MenuAdapter adapter = new MenuAdapter(getActivity(), getMenuItems(),
                // getMenuIcons());
                // optionsView.setAdapter(adapter);
                // optionsView
                // .setOnItemClickListener(new AdapterView.OnItemClickListener() {
                // @Override
                // public void onItemClick(AdapterView<?> parent, View view,
                // int position, long id) {
                // final NavigationController navigationController =
                // getNavigationController();
                // Section section = menuSections.get(position);
                // switch (section) {
                // case NEW_KICKON:
                // navigationController.navigateTo(
                // NewKickOnFragment.class,
                // Section.NEW_KICKON, null, false);
                // break;
                // case KICKONS:
                // navigationController.navigateTo(
                // KickOnsFragment.class, Section.KICKONS,
                // null, false);
                // break;
                // case SETTINGS:
                // navigationController.navigateTo(
                // SettingsFragment.class, Section.SETTINGS,
                // null, false);
                // break;
                // case INVITE:
                // navigationController.navigateTo(
                // InviteFragment.class, Section.INVITE, null,
                // false);
                // break;
                // }
                // navigationController.getNavigationMenu().hideMenu();
                // }
                // });
                //
                // rootView.findViewById(R.id.left_menu_sign_out).setOnClickListener(
                // new View.OnClickListener() {
                // @Override
                // public void onClick(View v) {
                // getNavigationController().getNavigationMenu()
                // .hideMenu();
                // WindowUtils.buildGenericDialog(
                // getActivity(),
                // getString(R.string.signout),
                // getString(R.string.signout_confirmation),
                // getString(R.string.no),
                // new DialogInterface.OnClickListener() {
                // @Override
                // public void onClick(DialogInterface dialog,
                // int which) {
                //
                // }
                // }, getString(R.string.yes),
                // new DialogInterface.OnClickListener() {
                // @Override
                // public void onClick(DialogInterface dialog,
                // int which) {
                // getNavigationController().exit();
                // }
                // }).show();
                // }
                // });

                return rootView;
        }

        // /**
        // * To save an image to Parse sever and get the info
        // */
        // public void createImageParseFile(Bitmap bm) {
        // ByteArrayOutputStream stream = new ByteArrayOutputStream();
        // bm.compress(Bitmap.CompressFormat.PNG, 100, stream);
        // final ParseFile pFile = new ParseFile("file", stream.toByteArray());
        // pFile.saveInBackground(new SaveCallback() {
        // @Override
        // public void done(ParseException e) {
        // try {
        // JSONObject anImageObjec = new JSONObject();
        // anImageObjec.put(URL, pFile.getUrl());
        // anImageObjec.put(NAME, pFile.getName());
        // anImageObjec.put(TYPE, "File");
        // jsonArrayBitmap.put(anImageObjec);
        // } catch (Exception e2) {
        // Logger.logMessage("createImageParseFile Error");
        // }
        // numberBitmapSaved += 1;
        // }
        // });
        // }

        private List<String> getMenuItems() {
                List<String> menuItems = new ArrayList<String>();
                menuItems.add(getString(R.string.menu_new_kickon));
                menuItems.add(getString(R.string.menu_kickons));
                menuItems.add(getString(R.string.menu_settings));
                menuItems.add(getString(R.string.menu_invite));

                return menuItems;
        }

        public LayoutInflater getSystemService(String layoutInflaterService) {
                // TODO Auto-generated method stub
                return null;
        }

        // private List<Integer> getMenuIcons() {
        // return Arrays.asList(R.drawable.menu_icon_newkickon,
        // R.drawable.menu_icon_kickons, R.drawable.menu_icon_settings,
        // R.drawable.menu_icon_invite);
        // }
}
